package com.chefu.operator.controller;


import com.chefu.common.pojo.PageResult;
import com.chefu.operator.pojo.ArticleRecommend;
import com.chefu.operator.service.RecommendService;
import com.chefu.operator.vo.RecommendListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/recommend")
@Slf4j
public class RecommendController {

    @Autowired
    private RecommendService recommendService;
    /**
     * 条件查询推文
     */
    @GetMapping
    public ResponseEntity<PageResult<RecommendListVo>> queryRecommendListByPage(
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "15") Integer rows,
            @RequestParam Map<String,Object> paramMap
            ){
      log.info("进入查询推文列表方法");
      PageResult<RecommendListVo> pageResult=recommendService.queryRecommendListByPage(page,rows,paramMap);
      return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增推文
     */
    @PostMapping
    public ResponseEntity<String> addArticleRecommend(
            @RequestBody ArticleRecommend articleRecommend
    ){
        log.info("进入新增推文方法");
        String result = recommendService.addArticleRecommend(articleRecommend);
        return ResponseEntity.ok(result);
    }

    /**
     * 推文数据回显
     */
    @GetMapping("{id}")
    public ResponseEntity<ArticleRecommend> queryArticleRecommendById(
            @PathVariable("id") Long id
    ){
        log.info("进入根据id查询推文详情");
        ArticleRecommend articleRecommend = recommendService.queryArticleRecommendById(id);
        return ResponseEntity.ok(articleRecommend);
    }

    /**
     * 修改推文
     */
    @PutMapping
     public ResponseEntity<String> updateArticleRecommend(
             @RequestBody ArticleRecommend articleRecommend
    ){
        log.info("进入根据id修改推文方法");
        String result = recommendService.updateArticleRecommend(articleRecommend);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除推文
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delArticleRecommend(
            @PathVariable("id") Long id
    ){
        log.info("进入删除推文方法");
        recommendService.delArticleRecomend(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
